import axios from "axios";
import { ElMessage } from "element-plus";
import { createRouter, createWebHistory } from 'vue-router'


const readUrl='http://localhost:8080/'
const writeUrl='http://localhost:8082/'
const tranceUrl=import.meta.env.VITE_TranceAPP_API_URL
//授权中心-读
const readBasic=axios.create({
    baseURL:readUrl,//基础路径
    timeout:5000
})
//请求拦截器
readBasic.interceptors.request.use(
    (config:any)=>{
        const token=localStorage.getItem("token");
        if(token&&config.headers){
            config.headers.Authorization=`Bearer ${token}`
        }
        return config
    },
    (error:any)=>{
        return Promise.reject(error)
    }
)
//响应拦截器
readBasic.interceptors.response.use(
    (res:any)=>{
        return res
    },
    (error:any)=>{
        if(error.response?.status===401)
            {
                ElMessage.error("登录已过期,请重新登录")
                setTimeout(() => {
                    localStorage.removeItem('token');
                    window.location.href = '/';
                }, 500);
            }
            else
            {
                ElMessage.error("请求失败");
                setTimeout(() => {
                    window.location.href = '/';
                }, 500);
            }
        return Promise.reject(error)
    }
)


//授权中心-写
const writeBasic=axios.create({
    baseURL:writeUrl,
    timeout:5000
})

writeBasic.interceptors.request.use(
    (config:any)=>{
        const token=localStorage.getItem("token")
        if(token&&config.headers){
            config.headers.Authorization=`Bearer ${token}`
        }
        return config
    },
    (error:any)=>{
        return Promise.reject(error)
    }
)

writeBasic.interceptors.response.use(
     (res:any)=>{
        return res
    },
    (error:any)=>{
        if(error.response?.status===401)
            {
                ElMessage.error("登录已过期,请重新登录")
                setTimeout(() => {
                    localStorage.removeItem('token');
                    window.location.href = '/';
                }, 500);
            }
            else
            {
                ElMessage.error("请求失败");
                setTimeout(() => {
                    window.location.href = '/';
                }, 500);
            }
        return Promise.reject(error)
    }
)

//溯源项目
const trance=axios.create({
    baseURL:tranceUrl,
    timeout:5000
})
//请求
trance.interceptors.request.use(
     (config:any)=>{
        const token=localStorage.getItem("token");
        if(token&&config.headers){
            config.headers.Authorization=`Bearer ${token}`
        }
        return config
    },
    (error:any)=>{
        return Promise.reject(error)
    }
)
//响应
trance.interceptors.response.use(
    (res:any)=>{
        return res
    },
    (error:any)=>{
        if(error.response?.status===401)
            {
                ElMessage.error("登录已过期,请重新登录")
                setTimeout(() => {
                    localStorage.removeItem('token');
                    window.location.href = '/';
                }, 500);
            }
            else
            {
                ElMessage.error("请求失败");
                setTimeout(() => {
                    // window.location.href = '/';
                }, 500);
            }
        return Promise.reject(error)
    }
)


const upload_Url='http://10.223.8.24:8023/api/FileUpload/UploadFile'


//库存
const inventoryReadurl='http://localhost:5215/'
const inventoryWriteurl='http://localhost:5171'


const inventoryRead=axios.create({
    baseURL:inventoryReadurl,
    timeout:5000
})

const inventoryWrite=axios.create({
    baseURL:inventoryWriteurl,
    timeout:5000
})




export{readBasic,writeBasic,trance,upload_Url,inventoryRead,inventoryWrite};

